<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.apply.info') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="150px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('organization.chinese_title')" prop="organization_id">
                <el-select v-model="dataForm.organization_id" :placeholder="$t('common.please_select') + $t('organization.chinese_title')" filterable clearable @change="loadMemberList">
                  <el-option v-for="(v, k) in organizationList" :label="v.chinese_title" :key="k" :value="v.id">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('member.chinese_nickname')" prop="member_id">
                <el-select v-model="dataForm.member_id" :placeholder="$t('common.please_select') + $t('member.chinese_nickname')" filterable clearable>
                  <el-option v-for="(v, k) in memberList" :label="v.chinese_nickname" :key="k" :value="v.id">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('organization.apply.inquiry_harbor')" prop="inquiry_harbor">
                <el-input
                  type="textarea"
                  v-model="dataForm.inquiry_harbor"
                  :rows="3"
                  show-word-limit
                  maxlength="200"
                  :placeholder="i18n('common.please_input') + i18n('organization.apply.inquiry_harbor')">
                </el-input>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:apply:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-apply-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/apply')

  const organizationList = ref([])

  const memberList = ref([])

  const dataForm = reactive({
    id: 0,
    organization_id: '',
    member_id: '',
    inquiry_harbor: ''
  })

  const dataRule = reactive({
    organization_id: [{
      required: true,
      message: i18n('organization.apply.rules.organization_id.require'),
      trigger: 'blur',
    }],
    member_id: [{
      required: true,
      message: i18n('organization.apply.rules.member_id.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.organization_id = data.data.organization_id
            dataForm.member_id = data.data.member_id
            dataForm.inquiry_harbor = data.data.inquiry_harbor
          }
        })
      }
    })
  }

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
      params: $http.adornParams({
        shipowner_id: 0,
        inquiry_receive: 2,
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadMemberList = (organization_id) => {
    $http({
      url: $http.adornUrl('/member/select'),
      method: 'get',
      params: $http.adornParams({
        organization_id: organization_id
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        memberList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadOrganizationList()
  })
</script>

<style lang="scss" scoped>
  .cx-view-preview {
    padding: 0 5px;
    max-width: 120px;
    max-height: 120px;
    width: 100%;
    height: 120px;
  }
  .admin_form_main .el-form-item {
    width: 100%;
  }
  .grid {
    display: grid;
    text-align: center;
    .filename {
      font-size: 14px;
      color: #666;
    }
  }
</style>
